﻿//
//  GNS_MakeLayer.cpp
//  MobileManage
//  FuncTion 遮罩层
//  Created by mr.l on 16/1/25.
//
//

#include "GNS_MakeLayer.h"
#include "GnsOften.h"

#define winsize Director::getInstance()->getVisibleSize()

USING_NS_CC;

GNS_MakeLayer::GNS_MakeLayer()
{
    pText=nullptr;
    pWaiting=nullptr;
}
GNS_MakeLayer::~GNS_MakeLayer()
{

}

GNS_MakeLayer* GNS_MakeLayer::CreateMakeLayer(std::string HintText)
{
//    auto pScene = Director::getInstance()->getRunningScene();
//    if (pScene ==nullptr) {
//        return 0;
//    }
    auto pMakeLayer = GNS_MakeLayer::create();
//    pScene->addChild(pMakeLayer);
    pMakeLayer->SetHintText(HintText);
    return pMakeLayer;
}

bool GNS_MakeLayer::init(){
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    auto pMakeLaye = LayerColor::create(Color4B(0,0,0,110));
    this->addChild(pMakeLaye);
    
    auto callback = [](Touch * ,Event *)
    {
        return true;
    };
    listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = callback;
    listener->setSwallowTouches(true);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
    
    
    // 3. return
    return true;
}
void GNS_MakeLayer::SetHintText(std::string HintText)
{

    pWaiting = Sprite::create("Waiting.png");
    pWaiting->setPosition(winsize.width/2,winsize.height/2);
    this->addChild(pWaiting);
    
    auto pRotateTo = RotateBy::create(0.5, 90);
    pWaiting->runAction(RepeatForever::create(pRotateTo));
    
    pText = Label::createWithSystemFont(CCGnsOften::getUtf8_DirectAssignmentInProgram(HintText), "", 28);
    pText->setPosition(Vec2(pWaiting->getPositionX(), pWaiting->getPositionY()-3*pText->getContentSize().height));
    pText->setColor(Color3B::RED);
    this->addChild(pText);
    

}

void GNS_MakeLayer::AutoClose(float dt)
{
    auto pTime = DelayTime::create(dt);
    auto pAutoClose = CallFunc::create([&](){
        Show(true);
    });
    auto pSeqAction = Sequence::createWithTwoActions(pTime, pAutoClose);
    pText->runAction(pSeqAction);
}

void GNS_MakeLayer::Show(bool bShow)
{
	this->removeAllChildrenWithCleanup(bShow);
    _eventDispatcher->removeEventListener(listener);
	// 	if (this->getParent() != nullptr)
	// 		this->getParent()->removeChild(this);
	this-> setVisible(bShow);

}